﻿Imports ControleMatricula.Negocio

Public Class FrmFuncionarioConsultar
    Dim db As New DataClassesDataContext()
    Friend RowFuncionario As SPFuncionarioConsultarResult

    Private Sub btnConsultar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConsultar.Click

        Dim retorno As List(Of SPFuncionarioConsultarResult)

        If IsNumeric(txtConsulta.Text) Then
            Dim id As Integer
            id = Convert.ToInt32(txtConsulta.Text)
            retorno = db.SPFuncionarioConsultar(id, "").ToList()
        Else
            retorno = db.SPFuncionarioConsultar(0, txtConsulta.Text).ToList()
        End If

        listFuncionario.DataSource = retorno
        listFuncionario.DisplayMember = "Nome"
        listFuncionario.ValueMember = "IDFuncionario"

        listFuncionario.Focus()

    End Sub

    Private Sub listFuncionario_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles listFuncionario.KeyDown

        If listFuncionario.Items.Count > 0 Then
            If e.KeyData = Keys.Enter Then
                Call Selecionar()
            End If
        End If

    End Sub

    Private Sub listFuncionario_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles listFuncionario.MouseUp
        If listFuncionario.Items.Count > 0 Then
            Call Selecionar()
        End If
    End Sub


    Sub Selecionar()

        Try
            Dim Retorno As List(Of SPFuncionarioConsultarResult)
            Retorno = db.SPFuncionarioConsultar(listFuncionario.SelectedValue, "").ToList()

            RowFuncionario = Retorno(0)

            Me.Close()

        Catch Er As Exception
            MsgBox(Er.Message, MsgBoxStyle.Critical, "Específica")
        Finally
            Me.Cursor = System.Windows.Forms.Cursors.Default
        End Try


    End Sub

    Private Sub txtConsulta_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtConsulta.KeyDown
        If e.KeyData = Keys.Enter Then
            btnConsultar_Click(sender, e)
        End If
    End Sub

    Private Sub txtConsulta_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtConsulta.TextChanged
        If txtConsulta.Text <> "" Then
            If Not IsNumeric(txtConsulta.Text) Then
                Label1.Text = "Nome"
            Else
                Label1.Text = "Código"
            End If
        Else
            Label1.Text = "Código / Nome"
        End If
    End Sub


End Class

